<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GatewayService
 *
 * @author gaitanoboeraeve
 */
class Service_GatewayService 
{
    public function __construct()
    {
        
    }

    /*// SCRIPT OM DATA TE LOGGEN
    public function _traceToLog()
    {
        $myFile = "testFile.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        $stringData = "Bobby Bopper\n";
        fwrite($fh, $stringData);
        $stringData = "Tracy Tanner\n";
        fwrite($fh, $stringData);
        fclose($fh);
    }*/
    
/*
 * 
 * USERS
 * 
 * 
 * 
 */  
 /**
     * @return Ahs_VO_User
     */
    public function getUser($email)
    {
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchByEmail($email);

        $userVO = $this->_toUserVO($user);

        return $userVO;
    }

    /**
     * @return array
     */
    public function getUsers()
    {   
        $userMapper = new Application_Model_UserMapper();
        $users = $userMapper->fetchAll();

        $userVOs = $this->_toUserVOs($users);

        return $userVOs;
    }

    /**
     * @return array
     */
    public function getUsersList()
    {
        return $this->getUsers();
    }

    /**
     * @param Application_Model_User $user
     * @return VO_User
     */
    private function _toUserVO(Application_Model_User $user)
    {
        $userVO = new VO_User();
        $userVO->id             = $user->Id;
        $userVO->email          = $user->Email;
        $userVO->displayname    = $user->Displayname;
        $userVO->worldId        = $user->WorldId;
        $userVO->levelId        = $user->LevelId;        
         
        

        return $userVO;
    }

    /**
     * @param array $users
     * @return array
     */
    private function _toUserVOs($users = array())
    {
        $userVOs = array();
        foreach ($users as $user) {
            $userVOs[] = $this->_toUserVO($user);
        }

        return $userVOs;
    }
    
/*
 * 
 * USERS HAS WORLD
 * 
 * 
 * 
 */  

    
/*
 * 
 * USERS HAS LEVEL
 * 
 * 
 * 
 */  
    
    public function setUserHasLevel($UserHasANewLevel)
    {
        $userHasWorldMapper = new Application_Model_UserHasWorldMapper();  
        
        //UPDATE LEVEL
        $userHasLevel = new Application_Model_UserHasLevel();
        
        $userHasLevel->Id       = $UserHasANewLevel->id;
	$userHasLevel->UserId   = $UserHasANewLevel->userId;
	$userHasLevel->Score    = $UserHasANewLevel->score;
	$userHasLevel->Time     = $UserHasANewLevel->time;
	$userHasLevel->Tries    = $UserHasANewLevel->tries;
        $userHasLevel->Unlocked = true;   
        
        $userHasLevelMapper = new Application_Model_UserHasLevelMapper();        
        $userHasLevelMapper->save($userHasLevel);

        //UPDATE WORLD -> niet op deze manier maar via ophalen vd vss levels
        /*$userHasWorld = new Application_Model_UserHasWorld();
        
        $userHasWorld->Id       = $UserHasANewLevel->currentWorldId;   
        $userHasWorld->UserId   = $UserHasANewLevel->userId;
        $userHasWorld->Score    += $UserHasANewLevel->score;            
        $userHasWorld->Unlocked = true;        
        
        $userHasWorldMapper->save($userHasWorld);*/
        
        if($UserHasANewLevel->nextLevelId > 0)
        {
            $userHasLevel = new Application_Model_UserHasLevel();
            
            $userHasLevel->Id       = $UserHasANewLevel->nextLevelId;   
            $userHasLevel->UserId   = $UserHasANewLevel->userId;
            $userHasLevel->Score    = 0;
            $userHasLevel->Time     = 0;
            $userHasLevel->Tries    = 0;
            $userHasLevel->Unlocked = true;   
            
            $userHasLevelMapper->save($userHasLevel);
        }
        //THERE IS NO NEXT LEVEL -> UNLOCK A NEW WORLD
        if($userHasGameElement->nextLevelId == null)
        {
            $userHasWorld = new Application_Model_UserHasWorld();            
            
            //UNLOCK WORLD
            $userHasWorld->Id       = $UserHasANewLevel->nextWorldId;   
            $userHasWorld->UserId   = $UserHasANewLevel->userId;
            $userHasWorld->Score    = 0;
            $userHasWorld->Unlocked = true;
            
            $userHasWorldMapper->save($userHasWorld);
            
            //UNLOCK FIRST LEVEL FROM WORLD
            $levelMapper = new Application_Model_LevelMapper();
            $levelId = $levelMapper->fetchFirstByWorldId($UserHasANewLevel->nextWorldId);
                        
            $userHasLevel = new Application_Model_UserHasLevel();
            
            $userHasLevel->Id       = $levelId;   
            $userHasLevel->UserId   = $UserHasANewLevel->userId;
            $userHasLevel->Score    = 0;
            $userHasLevel->Time     = 0;
            $userHasLevel->Tries    = 0;
            $userHasLevel->Unlocked = true;  
            
            $userHasLevelMapper->save($userHasLevel);
        }  
    }
    
    /*public function setUserHasGameElement($userHasGameElement)
    {
        $userHasLevel = new Application_Model_UserHasLevel();
        $userHasLevelMapper = new Application_Model_UserHasLevelMapper();     
        //$userHasWorldMapper = new Application_Model_UserHasWorldMapper(); 
        
        $userHasLevel->Id       = $userHasGameElement->id;
	$userHasLevel->UserId   = $userHasGameElement->userId;
	$userHasLevel->Score    = $userHasGameElement->score;
	$userHasLevel->Time     = $userHasGameElement->time;
	$userHasLevel->Tries    = $userHasGameElement->tries;
        $userHasLevel->Unlocked = true;   
                 
        $userHasLevelMapper->save($userHasLevel);
        
        //UNLOCK A NEW LEVEL
        if($userHasGameElement->nextLevelId > 0)
        {
            $userHasLevel = new Application_Model_UserHasLevel();
            
            $userHasLevel->Id       = $userHasGameElement->nextLevelId;   
            $userHasLevel->UserId   = $userHasGameElement->userId;
            $userHasLevel->Score    = 0;
            $userHasLevel->Time     = 0;
            $userHasLevel->Tries    = 0;
            $userHasLevel->Unlocked = true;   
            
            $userHasLevelMapper->save($userHasLevel);
        }
        //THERE IS NO NEXT LEVEL -> UNLOCK A NEW WORLD
        if($userHasGameElement->nextLevelId == -1)
        {
            $userHasWorld = new Application_Model_UserHasWorld();            
            
            //SEND ME THE NEXT WORLDID?
            $userHasWorld->Id       = $userHasGameElement->nextWorldId;   
            $userHasWorld->UserId   = $userHasGameElement->userId;
            $userHasWorld->Score    = $userHasGameElement->worldScore;            
            $userHasWorld->Unlocked = true;   
            
            $userHasWorldMapper->save($userHasWorld);
        }     
    }*/
/*
 * 
 * HIGHSCORES
 * 
 * 
 */    
    /**
     * @return Ahs_VO_Highscore
     */
    public function getHighscore()
    {
        $highscoreMapper = new Application_Model_HighscoreMapper();
        $highscore = $highscoreMapper->fetchById(2);
        
        $highscoreVO = $this->_toHighscoreVO($highscore);
        return $highscoreVO;
    }
    
    /**
     * @return array
     */
    public function getHighscores()
    {
        $highscoreMapper = new Application_Model_HighscoreMapper();
        $highscore = $highscoreMapper->fetchTopTen();
        
        $highscoreVOs = $this->_toHighscoreVOs($highscore);
        return $highscoreVOs;
    }

    /**
     * @return array
     */
    public function getHighscoresList()
    {
        return $this->getHighscores();
    }
    
    /**
     * @param Application_Model_Highscore $highscore
     * @return VO_Highscore 
     */
    private function _toHighscoreVO(Application_Model_Highscore $highscore)
    {
        $highscoreVO = new VO_Highscore();
        
        $user2 = new Application_Model_User();
        $user2->Id = "3";
        $user2->Email = "Ik@lol.be";
        
        $highscoreVO->highscore_id                  = $highscore->Id;
        $highscoreVO->highscore_number              = $highscore->Number;
        $highscoreVO->highscore_userId              = $highscore->UserId;
        $highscoreVO->highscore_userDisplayname     =  $highscore->User->Displayname;
        return $highscoreVO;
    }
    
    /**
     * @param array $highscores
     * @return array
     */
    private function _toHighscoreVOs($highscores = array())
    {
        $highscoreVOs = array();
        foreach ($highscores as $highscore) {
            $highscoreVOs[] = $this->_toHighscoreVO($highscore);
        }
        
        return $highscoreVOs;
    }

/*
 * 
 * WORLDS
 * 
 * 
 * 
 */    
    /**
     * @return Ahs_VO_World
     */
   
   /* public function getWorld()
    {
        $worldMapper = new Application_Model_WorldMapper();
        $world = $worldMapper->fetchById(2);
        
        $worldVO = $this->_toWorldVO($world);
        return $worldVO;
    }*/
    
    /**
     * @return integer
     */
    public function getNumberOfWorlds()
    {
        $worldMapper = new Application_Model_WorldMapper();        
        $numberOfWorlds = $worldMapper->getCount();        
        
        return $numberOfWorlds;
    }
    
    /**
     * @return array
     */
    public function getWorlds($userId)
    {
        $worldMapper = new Application_Model_WorldMapper();
        //$worlds = $worldMapper->fetchAll();
        $worlds = $worldMapper->fetchAllForUser($userId);
        
        $worldVOs = $this->_toWorldVOs($worlds);
        return $worldVOs;
    }

    /**
     * @return array
     */
    public function getWorldsList($userId)
    {
        return $this->getWorlds($userId);
    }
    
    /**
     * @param Application_Model_World $world
     * @return VO_World 
     */
    private function _toWorldVO(Application_Model_World $world)
    {
        $worldVO = new VO_World();
        $worldVO->world_id              = $world->Id;
        $worldVO->world_name            = $world->Name;
        $worldVO->world_description     = $world->Description;
        $worldVO->world_pictureUrl      = $world->pictureUrl;
        $worldVO->world_userScore       = $world->UserScore;
        $worldVO->world_userUnlocked    = $world->UserUnlocked;
        return $worldVO;
    }
    
    /**
     * @param array $worlds
     * @return array
     */
    private function _toWorldVOs($worlds = array())
    {
        $worldVOs = array();
        foreach ($worlds as $world) {
            $worldVOs[] = $this->_toWorldVO($world);
        }
        
        return $worldVOs;
    }          

/*
 * 
 * LEVELS
 * 
 * 
 * 
 */    
    /**
     * @return Ahs_VO_Level
     */
   
    /*public function getLevel()
    {
        $levelMapper = new Application_Model_LevelMapper();
        $level = $levelMapper->fetchById(2);
        
        $levelVO = $this->_toLevelVO($level);
        return $levelVO;
    }*/
    
    /**
     * @return integer
     */
    public function getNumberOfLevels($worldId)
    {
        $levelMapper = new Application_Model_LevelMapper();
        $numberOfLevels = $levelMapper->getCount($worldId);        
        
        return $numberOfLevels;
    }
    
     public function getTotalNumberOfLevels()
    {
        $levelMapper = new Application_Model_LevelMapper();
        $numberOfLevels = $levelMapper->getTotalCount();        
        
        return $numberOfLevels;
    }
    
    /**
     * @return array
     */
    public function getLevelsByWorldId($worldId, $userId)
    {
                
        //PER WERELD WERKT NOG NIET
        $levelMapper = new Application_Model_LevelMapper();
        $levels = $levelMapper->fetchAllForUser($worldId, $userId);
        
        $levelVOs = $this->_toLevelVOs($levels);
        return $levelVOs;
    }

    /**
     * @return array
     */
    public function getLevelsListByWorldId($worldId, $userId)
    {
        return $this->getLevelsByWorldId($worldId, $userId);
    }
    
    /**
     * @param Application_Model_Level $level
     * @return VO_Level 
     */
    private function _toLevelVO(Application_Model_Level $level)
    {
        $levelVO = new VO_Level();
        $levelVO->level_id              = $level->Id;
        $levelVO->level_name            = $level->Name;
        $levelVO->level_numberofcards   = $level->NumberOfCards;
        $levelVO->level_time            = $level->Time;
        //$levelVO->level_worldId         = $level->WorldId;
        //$levelVO->level_world           = $level->World;
        $levelVO->level_pictureUrl      = $level->PictureUrl;
        $levelVO->level_userScore       = $level->UserScore;
        $levelVO->level_userTime        = $level->UserTime;
        $levelVO->level_userTries       = $level->UserTries;
        $levelVO->level_userUnlocked    = $level->UserUnlocked;
        return $levelVO;
    }
    
    /**
     * @param array $levels
     * @return array
     */
    private function _toLevelVOs($levels = array())
    {
        $levelVOs = array();
        foreach ($levels as $level) {
            $levelVOs[] = $this->_toLevelVO($level);
        }
        
        return $levelVOs;
    }
}  
?>
